/**
 * @对于树形数据得理解
 * @将树形数据进行扁平化
 */

let treeData = [
    {
      id: 1,
      name: "海门盟",
      children: [
        {
          id: 3,
          name: "轴天意",
          pid: 1,
        },
        {
          id: 4,
          name: "轴爱国",
          pid: 1,
          children: [{ id: 10, name: "轴孙儿", pid: 4 }],
        },
      ],
    },
    {
      id: 2,
      name: "轴式唇",
    },
  ];
  
  function treeSplit(treeData) {
    let Arraycontainer = [];
    function recursion(treeData) {
      treeData.forEach((tree) => {
        if (tree.children) {
          recursion(tree.children);
        }
        Arraycontainer.push({
          id: tree.id,
          title: tree.name,
          pid: tree.pid ?? "",
        });
      });
    }
    recursion(treeData);
    return Arraycontainer;
  }
  let res = treeSplit(treeData);
  
  /**
   * @扁平得数据变成树形数据
   * @思路 []
   */
  
  function converTree(res) {
    let tree = [];
    //当pid和id相等是不是找到父亲了？
    res.forEach((item) => {
      let parent = res.find((v) => item.pid === v.id);
      if (parent) {
        if (!parent.children) {
          parent.children = [];
        }
        parent.children.push(item);
      } else {
        tree.push(item);
      }
    });
    return tree;
  }
  let r = converTree(res);
  console.log(r);
  